package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CarPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.BusCar;
import com.southwind.entity.BusCustomer;
import com.southwind.form.ImgForm;
import com.southwind.service.BusCarService;
import com.southwind.service.BusCustomerService;
import com.southwind.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/car")
public class BusCarController {

    @Autowired
    private BusCarService carService;
    @Value("${upload.numberUrl}")
    private String numberUrl;
    @Value("${upload.urlPrefix}")
    private String urlPrefix;
    @Autowired
    private BusCustomerService customerService;

    @GetMapping("/list")
    public Map list(CarPageModel carPageModel){
        QueryWrapper<BusCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(carPageModel.getCarnumber()), "carnumber", carPageModel.getCarnumber())
                    .eq(carPageModel.getIsrenting() != null, "isrenting", carPageModel.getIsrenting());
        Page<BusCar> page = new Page<>(carPageModel.getPage(), carPageModel.getLimit());
        Page<BusCar> resultPage = this.carService.page(page, queryWrapper);
        return ResultObj.resultData(resultPage.getRecords(), resultPage.getTotal());
    }

    @PostMapping("/uploadImg")
    public ResultObj uploadImg(@RequestBody ImgForm imgForm) throws Exception{
        String newFileName = UUID.randomUUID()+"."+imgForm.getExtName();
        String path = this.numberUrl + newFileName;
        Base64Util.decoderBase64File(imgForm.getFileBase64(),path);
        Map<String,Object> map = new HashMap<>();
        map.put("src", urlPrefix + "car_rental/number/" +newFileName);
        return new ResultObj(0, map);
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody BusCar car){
        //验证车牌号是否唯一
        BusCar one = this.carService.getById(car.getCarnumber());
        if(one != null) return ResultObj.CAR_NUMBER_ERROR;
        car.setIsrenting(0);
        boolean save = this.carService.save(car);
        if(save) return ResultObj.CAR_ADD_SUCCESS;
        return ResultObj.CAR_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody BusCar car){
        boolean updateById = this.carService.updateById(car);
        if(updateById) return ResultObj.CAR_UPDATE_SUCCESS;
        return ResultObj.CAR_UPDATE_ERROR;
    }

    @DeleteMapping("/delete")
    public ResultObj delete(String carnumber){
        boolean removeById = this.carService.removeById(carnumber);
        if(removeById) return ResultObj.CAR_DELETE_SUCCESS;
        return ResultObj.CAR_DELETE_ERROR;
    }

    @GetMapping("/rentList")
    public ResultObj rentList(String identity,Integer page,Integer limit){
        //校验身份证号
        BusCustomer customer = this.customerService.getById(identity);
        if(customer == null) return ResultObj.RENT_IDENTITY_ERROR;
        //查询可租车辆
        QueryWrapper<BusCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isrenting", 0);
        Page<BusCar> carPage = new Page<>(page, limit);
        Page<BusCar> resultPage = this.carService.page(carPage, queryWrapper);
        return new ResultObj(resultPage.getRecords(), resultPage.getTotal());
    }

}

